package com.gdssoft.framework.action.hydroelectricity;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import com.gdssoft.core.web.action.BaseAction;

public class ImportExcelAction extends BaseAction {
	private File file;
	private String fileFileName;
	private String fileName;
	private String filePath;

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}

	public String checkXLSFormat() {

		System.out.println("checkXLSFormat start");

		int message;
		// 通过文件流过去要操作Excel的句柄
		FileInputStream fi;
		Workbook wb = null;
		try {
			fi = new FileInputStream(file);

			wb = new HSSFWorkbook(fi);
		} catch (Exception e1) {
			System.out.println("文件类型错误");
			message = 2;
		}

		try {
			// 获取Excel实例的第一页
			Sheet sheet = wb.getSheetAt(0);
			int rowNum = sheet.getLastRowNum() + 1;
			for (int i = 0; i < rowNum; i++) {
				Row row = sheet.getRow(i);
				int len = row.getLastCellNum();
				for (int j = 0;j < len; j++) {
					Cell cell = row.getCell(j);
					System.out.println(cell.getStringCellValue());
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("获取错误");
			message = 0;
		}
		message = 1;
		setJsonString("{success:true,message:" + message + "}");

		return SUCCESS;
	}

}
